package src.java;
import java.sql.*;
import org.json.JSONObject;
public class dcjson {

    public static void main(String[] args) {
        Connection conn;
        Statement stmt;
        ResultSet rs;
        JSONObject json;
        try {
//连接数据库
            conn = DriverManager.getConnection("jdbc:mysql://localhost/bh?user=root&password=root");
            stmt = conn.createStatement();
            rs = stmt.executeQuery("SELECT * FROM bhall");
//将ResultSet中的数据转化为JSON格式
            while (rs.next()) {
                json = new JSONObject();
                json.put("id", rs.getString("id"));
                json.put("scollege", rs.getString("scollege"));
                json.put("sclass", rs.getString("sclass"));
                json.put("sname", rs.getString("sname"));
                json.put("sid", rs.getString("sid"));
                System.out.println(json.toString());
            }
//关闭连接
            rs.close();
            stmt.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
